using Microsoft.AspNetCore.Identity;
using Services.Initial.Extensions;
using Services.Initial.Options;
using Services.Identity.Domain.Entities;
using Services.Identity.Infrastructure;
using Services.Identity.WebApi.Service;

var builder = WebApplication.CreateBuilder(args);

builder.ConfigureDbConfiguration();
builder.ConfigureExtraServices(new InitializerOptions("../Log/WebApi","Identity.WebApi"));


builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new() { Title = "Identity.WebAPi", Version = "V1" });
});
//添加数据保护
builder.Services.AddDataProtection();
//启动signalr
builder.Services.AddSignalR();
//登录、注册的项目除了要启用WebApplicationBuilderExtensions中的初始化之外，还要如下的初始化
//不要用AddIdentity，而是用AddIdentityCore
//因为用AddIdentity会导致JWT机制不起作用，AddJwtBearer中回调不会被执行，因此总是Authentication校验失败
var identityBuilder = builder.Services.AddIdentityCore<User>(options =>
{
    {
        options.Password.RequireDigit = false;
        options.Password.RequireLowercase = false;
        options.Password.RequireNonAlphanumeric = false;
        options.Password.RequireUppercase = false;
        options.Password.RequiredLength = 6;
        //不能设定RequireUniqueEmail，否则不允许邮箱为空
        //options.User.RequireUniqueEmail = true;
        //以下两行，把GenerateEmailConfirmationTokenAsync验证码缩短
        options.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultEmailProvider;
        options.Tokens.EmailConfirmationTokenProvider = TokenOptions.DefaultEmailProvider;
    }
});
identityBuilder = new IdentityBuilder(identityBuilder.UserType, typeof(Role), builder.Services);
identityBuilder.AddEntityFrameworkStores<MyIdentityDbContext>().AddDefaultTokenProviders().AddRoleManager<RoleManager<Role>>().AddUserManager<MyIdentityUserManager>();

// 注册局域网访问IP端口
builder.WebHost.ConfigureKestrel(serverOptions =>
{
    serverOptions.ListenAnyIP(7221, listenOptions =>
    {
        listenOptions.UseHttps();
    });
});

var app = builder.Build();

app.UseMyDefault("Identity");
app.MapHub<UserMessageHub>("/Hubs/ChatRoomHub");
app.Run();
